package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    //注入公寓图片信息表的service
    @Autowired
    private GraphInfoService graphInfoService;
    //注入公寓配套信息表的service
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    //注入公寓标签信息表的service
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    //注入公寓杂费表的service
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;
    @Resource
    private FeeValueMapper feeValueMapper;
    @Resource
    private RoomInfoMapper roomInfoMapper;

    //保存或更新公寓信息
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //1.首先获取apartmentSubmitVo的id 如果能拿到id代表当前是修改的方法 否则是删除的方法
        Boolean isUpdate = apartmentSubmitVo.getId() != null;
        //2.先将ApartmentInfo当时的数据保存到数据库表中，再去处理子类当中的属性
        super.saveOrUpdate(apartmentSubmitVo);
        //3.判断当前如果执行的是修改方法
        if (isUpdate) {
            //3.1.删除图片列表
            LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
            /*构造的第一个删除条件为:删除图片类型为公寓(ItemType.APARTMENT)的这张图片
            第一个参数是获取图片当中类型信息：判断是公寓图片信息 还是房间的图片信息
            第二个参数代表的是我们要删除的图片类型：因为要删除的是公寓当中的图片信息，因此传入公寓图片信息的枚举对象实例
            * */
            graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            /*构造的第二个删除条件为:公寓id为apartmentSubmitVo当中获取到的id*/
            graphInfoQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(graphInfoQueryWrapper);

            //3.2.删除配套列表
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
            /*删除条件为公寓的id为apartmentSubmitVo当中获取到的id*/
            apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityQueryWrapper);
            //3.3.删除标签列表
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
            /*删除条件为：公寓的id为apartmentSubmitVo当中获取到的id*/
            apartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId, apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelQueryWrapper);
            //3.4.删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
            /*删除条件为：公寓的id为apartmentSubmitVo当中获取到的id*/
            apartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId, apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);
        }
        //4.删除之后重新插入子类列表信息，如果添加的方法，则不会执行上面的删除逻辑，直接添加子类信息即可
        //4.1.添加图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        //判断当前图片列表是否为空，不为空则进行插入操作
        if (!CollectionUtils.isEmpty(graphVoList)) {
            //构建图片信息集合 将List<GraphVo>转换为List<GraphInfo>类型
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            //遍历图片列表
            for (GraphVo graphVo : graphVoList) {
                //构建图片信息
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                //将图片信息添加到集合中
                graphInfoList.add(graphInfo);
            }
            //因为saveBatch方法当中需要传递的集合类型对象为List<GraphInfo>，因此需要先将List<GraphVo>转换为List<GraphInfo>类型
            graphInfoService.saveBatch(graphInfoList);
        }
        //4.2.添加配套列表
        /*获取配套信息列表id*/
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilityList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .facilityId(facilityInfoId)
                        .build();
                apartmentFacilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }
        //4.3.添加标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        ArrayList<ApartmentLabel> apartmentLabelList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(apartmentLabelList)) {
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .labelId(labelId)
                        .build();
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }


        //4.4.添加杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(apartmentFeeValueList)) {
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .feeValueId(feeValueId)
                        .build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }


    }

    //根据条件分页查询公寓列表
    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(page, queryVo);
    }

    //根据id查询公寓详情信息内容
    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //1.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        if (apartmentInfo == null) {
            return null;
        }
        //2.查询公寓信息下的图片列表信息
        List<GraphVo> graphInfoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);
        //3.查询公寓信息下的标签列表信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLisByApartmentId(id);
        //4.查询公寓信息下的配套列表信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectLisByApartmentId(id);
        //5.查询公寓信息下的杂费列表信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectLisByApartmentId(id);
        //结果组装
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //将apartmentInfo当中封装的字段属性复制到apartmentDetailVo当中
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        return apartmentDetailVo;
    }

    //根据id删除公寓信息内容
    @Override
    public void removeApartmentById(Long id) {
        //判断该公寓下是否存在房间的信息
        LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(queryWrapper);
        if (count > 0) {
            //终止删除，并且响应提示信息
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }
        super.removeById(id);
        //3.1.删除图片列表
        LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
            /*构造的第一个删除条件为:删除图片类型为公寓(ItemType.APARTMENT)的这张图片
            第一个参数是获取图片当中类型信息：判断是公寓图片信息 还是房间的图片信息
            第二个参数代表的是我们要删除的图片类型：因为要删除的是公寓当中的图片信息，因此传入公寓图片信息的枚举对象实例
            * */
        graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        /*构造的第二个删除条件为:公寓id为apartmentSubmitVo当中获取到的id*/
        graphInfoQueryWrapper.eq(GraphInfo::getId, id);
        graphInfoService.remove(graphInfoQueryWrapper);
        //3.2.删除配套列表
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
        /*删除条件为公寓的id为apartmentSubmitVo当中获取到的id*/
        apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(apartmentFacilityQueryWrapper);
        //3.3.删除标签列表
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelQueryWrapper = new LambdaQueryWrapper<>();
        /*删除条件为：公寓的id为apartmentSubmitVo当中获取到的id*/
        apartmentLabelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(apartmentLabelQueryWrapper);
        //3.4.删除杂费列表
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueQueryWrapper = new LambdaQueryWrapper<>();
        /*删除条件为：公寓的id为apartmentSubmitVo当中获取到的id*/
        apartmentFeeValueQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(apartmentFeeValueQueryWrapper);
    }


}




